package com.school.demo.controller;


import com.school.demo.model.auto.ReturnMessage;
import com.school.demo.model.auto.ReturnUser;
import com.school.demo.model.auto.User;
import com.school.demo.service.IUserService;
import com.school.demo.util.GsonUtil;
import com.school.demo.util.PasswordUtil;
import com.school.demo.util.RestfulInfo;
import com.school.demo.util.Status;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author Charlotte
 * @since 2021-03-01
 */
@Api(tags = "用户接口控制类")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private PasswordUtil passwordHelper;


    @ApiOperation(value="登录", notes="")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户登录账号，如phone，qq号，微信号，邮箱等，现在只有phone",name = "username",dataType = "String",required = true),
            @ApiImplicitParam(value = "登录密码",name = "password",dataType = "String",required = true)
    })
    @PostMapping("/doLogin")
    public Object doLogin(@RequestParam String username, @RequestParam String password) {
        //暂定username为phone
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            ReturnMessage returnMessage = new ReturnMessage();
            returnMessage.setCode(Status.SUCCESS.code);
            returnMessage.setMsg(Status.SUCCESS.message);
        } catch (IncorrectCredentialsException ice) {
            return "password error!";
        } catch (UnknownAccountException uae) {
            return "username error!";
        }

        //phone
        User user = userService.findByPhone(username);
        ReturnUser returnUser = new ReturnUser(user);
        subject.getSession().setAttribute("user", returnUser);
        return "SUCCESS";
    }

    @GetMapping("/forelogout")
    public String logout( ) {
        Subject subject = SecurityUtils.getSubject();
        if(subject.isAuthenticated()) {
            subject.logout();
        }
        return "redirect:index";
    }

    @ApiOperation(value="注册用户", notes="")
    @ApiImplicitParam(value = "注册用户信息",name = "user",dataType = "User",required = true)
    @RequestMapping(value={"/register1_"}, method= RequestMethod.POST)
    public Object register(@RequestParam("user")User user) {
        try{
            /**
             * 进行数据验证
             */
//            passwordHelper.encryptPassword(user);
            User user1 = userService.findByPhone(user.getMobile());
            if (user1 != null){
                return new RestfulInfo(Status.REGISTER_USER_EXIST);
            }else if(user1.getName().length()>20){
                return new RestfulInfo(Status.REGISTER_USER_FAILE);
            }

            /**
             * 后面有其他登录模式的话，需要同时生成一个account保存不同注册模式
             */
            userService.save(user);
            return "SUCCESS";
        }catch (Exception e){
            e.printStackTrace();
            return "Fail";
        }
    }
}
